package com.app.freshmenapp.service.impl;

import com.app.freshmenapp.dao.SchoolMatesDao;
import com.app.freshmenapp.entity.SchoolMates;
import com.app.freshmenapp.service.SchoolMatesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

/**
 * @Auther: Administrator
 * @Date: 2019.07.20 17:07
 * @Description: 同学信息服务层
 */

@Service
@SuppressWarnings("all")
public class SchoolMatesServiceImpl implements SchoolMatesService {

    @Autowired
    private SchoolMatesDao schoolMatesDao;

    @Override
    public boolean addSchoolMates(SchoolMates schoolMates) {
        SchoolMates schoolMates1 = schoolMatesDao.saveAndFlush(schoolMates);
        if(schoolMates1!=null){
            return true;
        }
        return false;
    }

    @Override
    public boolean deleteSchoolMates(Integer id) {
        if(schoolMatesDao.existsById(id)){
            schoolMatesDao.deleteById(id);
            return true;
        }
        return false;
    }

    @Override
    public boolean updateSchoolMates(SchoolMates schoolMates) {
        SchoolMates schoolMates1 = schoolMatesDao.saveAndFlush(schoolMates);
        if(schoolMates1!=null){
            return true;
        }
        return false;
    }

    @Override
    public SchoolMates findById(Integer id) {
        if (schoolMatesDao.existsById(id)){
            Optional<SchoolMates> optionalSchoolMates=schoolMatesDao.findById(id);
            return optionalSchoolMates.get();
        }
        return null;
    }

    @Override
    public List<SchoolMates> findAllSchoolMates() {
    	
    
        return schoolMatesDao.findAll();
    }

	@Override
	public List<SchoolMates> findAllSchoolMatesByClassName(SchoolMates schoolMate) {
		Example<SchoolMates> example = Example.of(schoolMate);
		return schoolMatesDao.findAll(example);
	}
}
